/***************************************************************************
 *   Copyright (C) 2007 by Alexandre Masselot,,,   *
 *   alex@alex-laptop   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "utils/runlcmsmsreadermgf.h"
#include "utils/deltamassbuilder.h"

#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>

using namespace std;
using namespace boost::iostreams;
using namespace bol;

#define STRINGIFY(x) XSTRINGIFY(x)
#define XSTRINGIFY(x) #x

void printNearPrecursor(const double mass);
static RunLcmsms run;

int main(int argc, char **argv){
#ifdef HAVE_BOOST_IOSTREAMS
  string file;
  if(argc>1){
    file=string(argv[1]);
  }else{
    file=STRINGIFY(DATADIR) "/a.mgf.gz";
  }
  filtering_istream* input = new filtering_istream(gzip_decompressor() | file_source(file));


  RunLcmsmsReaderMGF reader;
  reader.read(run, input);

  for(int i=0; i<run.msmsCollection().size(); i++){
    cout<<run.msmsCollection()[i]<<endl;
  }

  run.buildIndex();
  DeltaMassBuilder builder;
  run.msmsCollection().index()->setTolerance(builder.deltamassInstance("5Da"));
  printNearPrecursor(2163.6);
#else
  cerr << "boost iostreams isn't compiled in" << endl;
#endif
  return 0;
}

void printNearPrecursor(const double m){
  vector<const SpectrumLcmsmsMassIndexAnchor*>::const_iterator first;
  vector<const SpectrumLcmsmsMassIndexAnchor*>::const_iterator last;
  run.msmsCollection().index()->getAnchors(m, first, last);
  cout<<"peaks close to "<<m<<endl;
  for(vector<const SpectrumLcmsmsMassIndexAnchor*>::const_iterator it=first; it<last; it++){
    cout<<(*it)->charge()<<"+ => "<<  *((Peak*)(*it)->spectrum())<<endl;
  }

}
